package com.beagle.njddapp.ui.presenter;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BaseApplication;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.OilApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.UploadBean;
import com.beagle.njddapp.bean.oil.OilBean;
import com.beagle.njddapp.net.BaseHttpResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.OilAddView;
import com.blankj.utilcode.util.ApiUtils;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;


import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;


public class OilAddPresenter extends BasePresenter<OilAddView> {
    private OilApiService oilApiService;
    private ApiService apiService;

    public OilAddPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        oilApiService = retrofit.create(OilApiService.class);
        apiService = retrofit.create(ApiService.class);

    }


    public void addOil(OilBean oilOrderBean) {
        Gson gson = new Gson();
        String strEntity=gson.toJson(oilOrderBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        oilApiService.resetOilCar(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse o) {
                getView().showSuccess();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }
    public void updateOilCar(OilBean oilOrderBean) {
        Gson gson = new Gson();
        String strEntity=gson.toJson(oilOrderBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        oilApiService.updateOilCar(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse>() {
            @Override
            public void success(BaseOilHttpResponse o) {
                getView().showSuccess();
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }


    public void upload(String path){

        Luban.with(BaseApplication.getInstance().getApplicationContext())
            .load(path)// 传人要压缩的图片列表
                .ignoreBy(100)                                  // 忽略不压缩图片的大小
                .setTargetDir(BaseApplication.getInstance().getCacheDir().getAbsolutePath())                        // 设置压缩后文件存储位置
            .setCompressListener(new OnCompressListener() {
                @Override
                public void onStart() {

                }

                @Override
                public void onSuccess(File file) {
                    String name=path.substring(path.lastIndexOf("/")+1,path.length()-1);
                    LogUtils.d(name);
                    RequestBody body=RequestBody.create(MediaType.parse("multipart/form-data"),file);//表单类型
                    MultipartBody.Builder builder = new MultipartBody.Builder()
                            .setType(MultipartBody.FORM);
                    builder.addFormDataPart("file",file.getName(),body); //添加图片数据，body创建的请求体
                    List<MultipartBody.Part> parts=builder.build().parts();
                    apiService.uploadFile(parts).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseHttpResponse<UploadBean>>() {
                        @Override
                        public void success(BaseHttpResponse<UploadBean> uploadBeanBaseHttpResponse) {
                            if (uploadBeanBaseHttpResponse.data!=null){
                                getView().UploadSuccess(uploadBeanBaseHttpResponse.data);
                            }else {
                                getView().onError("请重试");
                            }
                        }

                        @Override
                        public void error(int code, String message) {
                            getView().onError(message);
                        }
                    });
                }

                @Override
                public void onError(Throwable e) {

                }
            }).launch();

    }

}